﻿using System;
using System.Linq;
using System.Linq.Expressions;
using SaiLing.Repository;
using SaiLing.SingleSignOn.Domain.Entities.System;
using SaiLing.SingleSignOn.Domain.Interfaces.System;
using System.Threading.Tasks;
using SaiLing.SingleSignOn.Domain.Entities.DataTransferObject;
using SaiLing.Paging;
using SaiLing.Domain.Specification;

namespace SaiLing.SingleSignOn.Repository.Providers.System
{
    public class ModuleRepository : BaseRepository<Module>, IModuleRepository
    {
        public ModuleRepository(IDatabaseFactory<ProjectDbContext> databaseFactory) : base(databaseFactory)
        {
        }


        public  Task<PageData<ModuleDTO>> PageDataList<TS>(int pageIndex, int pageSize, ISpecification<Module> specification, Expression<Func<Module, TS>> orderByExpression, bool isDesc)
        {
            var result = from x in DataContext.Set<Module>().Where(specification.SatisfiedBy())
                         select new ModuleDTO
                         {
                             Actions = x.Actions,
                             AppId = x.AppId,
                             AppName = x.App.Name,
                             Area = x.Area,
                             Background = x.Background,
                             ExecAction = x.ExecAction,
                             ExecController = x.ExecController,
                             Icon = x.Icon,
                             Id = x.Id,
                             Name = x.Name,
                             ParentId = x.ParentId,
                             Sort = x.Sort,
                             Visible = x.Visible
                         };
            return result.ToPageData(pageIndex, pageSize);
        }
    }
}
